# Compare the emotional reactions of the top two presidential parties in non-concurrent presidential and legislative
# elections. See panel A of Table S.1, Appendix S.




#############
# load data #
#############

library(ggplot2)
library(ggpubr)
library(stargazer)

# post level data of top two parties in presidetial and legisaltive elections
load("PSRM Replication Files/EmotionDifferentElectionsComparison.RData")

colnames(presidential_toptwo)

# check number of posts and elections
table(presidential_toptwo$electiontype)
# Legislative Presidential 
#        5641         3400
table(unique(presidential_toptwo[,c("election", "electiontype")])$electiontype)
#Legislative Presidential 
#         24           18 

table(presidential_legislative_toptwo$electiontype)
# Legislative Presidential 
#        6574         3400
table(unique(presidential_legislative_toptwo[,c("election", "electiontype")])$electiontype)
#Legislative Presidential 
#         24           18

summary(presidential_toptwo[,c("loveprop", "angryprop")])
#         loveprop         angryprop      
# Min.   : 0.0000   Min.   : 0.0000  
# 1st Qu.: 0.7752   1st Qu.: 0.0000  
# Median : 3.9813   Median : 0.1859  
# Mean   : 6.0288   Mean   : 2.2086  
# 3rd Qu.: 9.0737   3rd Qu.: 0.9461  
# Max.   :66.6667   Max.   :87.5743

summary(presidential_legislative_toptwo[,c("loveprop", "angryprop")])
#         loveprop         angryprop      
# Min.   : 0.0000   Min.   : 0.0000  
# 1st Qu.: 0.8812   1st Qu.: 0.0000  
# Median : 3.9514   Median : 0.1782  
# Mean   : 6.1136   Mean   : 2.0291  
# 3rd Qu.: 9.0218   3rd Qu.: 0.9901  
# Max.   :69.4701   Max.   :79.2507




############################################################################
# compare reactions of top two presidential parties in different elections #
############################################################################

presidential_toptwo$electiontype <- factor(presidential_toptwo$electiontype,
                                           levels = c("Presidential", "Legislative"))

# t tests
(plove_pre <- t.test(presidential_toptwo$loveprop_demean[which(presidential_toptwo$post == 0)]
                     ~ presidential_toptwo$electiontype[which(presidential_toptwo$post == 0)]))
(plove_post <- t.test(presidential_toptwo$loveprop_demean[which(presidential_toptwo$post == 1)]
                      ~ presidential_toptwo$electiontype[which(presidential_toptwo$post == 1)]))
(pangry_pre <- t.test(presidential_toptwo$angryprop_demean[which(presidential_toptwo$post == 0)]
                      ~ presidential_toptwo$electiontype[which(presidential_toptwo$post == 0)]))
(pangry_post <- t.test(presidential_toptwo$angryprop_demean[which(presidential_toptwo$post == 1)]
                       ~ presidential_toptwo$electiontype[which(presidential_toptwo$post == 1)]))

# function to extract t test results
getT <- function(ttest){
  ttab <- data.frame(matrix(NA, nrow = 1, ncol = 4))
  colnames(ttab) <- c("mean1", "mean2", "p", "ci")
  
  est <- ttest$estimate
  ttab$mean1 <- est[1]
  ttab$mean2 <- est[2]
  
  ttab$p <- ttest$p.value
  
  ttab$ci <- paste0("[", paste(round(ttest[[4]], 2), collapse = ", "), "]")
  
  return(ttab)
}

# make table
pall <- rbind(getT(plove_pre), getT(plove_post), getT(pangry_pre), getT(pangry_post))
pall$variable <- c("Pre-Election Love", "Post-Election Love", "Pre-Election Angry", "Post-Election Angry")

pall$difference <- round(pall$mean1 - pall$mean2, 2)
pall$mean1 <- round(pall$mean1, 2)
pall$mean2 <- round(pall$mean2, 2)

# table S.1 panel A
stargazer(t(t(pall[,c(5,1,2,6,4)])),
          covariate.labels = c("Variable", "Presidential Elections Mean", 
                               "Legislative Elections Mean", "Difference"))




#########################################################################################
# compare reactions of top two presidential and legislative parties in the same country #
#########################################################################################

presidential_legislative_toptwo$electiontype <- factor(presidential_legislative_toptwo$electiontype, 
                                                       levels = c("Presidential", "Legislative"))

# t tests
(llove_pre <- t.test(presidential_legislative_toptwo$loveprop_demean[which(presidential_legislative_toptwo$post == 0)]
                     ~ presidential_legislative_toptwo$electiontype[which(presidential_legislative_toptwo$post == 0)]))
(llove_post <- t.test(presidential_legislative_toptwo$loveprop_demean[which(presidential_legislative_toptwo$post == 1)]
                      ~ presidential_legislative_toptwo$electiontype[which(presidential_legislative_toptwo$post == 1)]))
(langry_pre <- t.test(presidential_legislative_toptwo$angryprop_demean[which(presidential_legislative_toptwo$post == 0)]
                      ~ presidential_legislative_toptwo$electiontype[which(presidential_legislative_toptwo$post == 0)]))
(langry_post <- t.test(presidential_legislative_toptwo$angryprop_demean[which(presidential_legislative_toptwo$post == 1)]
                       ~ presidential_legislative_toptwo$electiontype[which(presidential_legislative_toptwo$post == 1)]))

# make table
lall <- rbind(getT(llove_pre), getT(llove_post), getT(langry_pre), getT(langry_post))
lall$variable <- c("Pre-Election Love", "Post-Election Love", "Pre-Election Angry", "Post-Election Angry")

lall$difference <- round(lall$mean1 - lall$mean2, 2)
lall$mean1 <- round(lall$mean1, 2)
lall$mean2 <- round(lall$mean2, 2)

# table S.1 panel B
stargazer(t(t(lall[,c(5,1,2,6,4)])),
          covariate.labels = c("Variable", "Presidential Elections Mean", 
                               "Legislative Elections Mean", "Difference"))


# check list of elections
elections <- unique(presidential_legislative_toptwo[,c("election", "electiontype",
                                                       "country", "electionyear")])
elections <- elections[order(elections$electiontype, elections$country, elections$electionyear),]

elections$cy <- paste0(elections$country, " (", elections$electionyear, ")")

paste(elections$cy[which(elections$electiontype == "Presidential")], collapse = ", ")
paste(elections$cy[which(elections$electiontype == "Legislative")], collapse = ", ")
